﻿using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule
{
    public class PurchaseReturnSpecification : ISpecification<PurchaseReturn, PurchaseReturnSearch>
    {
        public PurchaseReturnSearch SearchParams
        {
            get;
            set;
        }

        public System.Linq.Expressions.Expression<Func<PurchaseReturn, bool>> Expression
        {
            get
            {
                IQueryBuilder<PurchaseReturn> queryBuilder = QueryBuilder.Create<PurchaseReturn>();

                if (SearchParams.IsNotNull())
                {
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(pr => pr.Id, SearchParams.ID);
                    }
                    if (SearchParams.PurchaseId.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(pr => pr.Purchase.Id, SearchParams.PurchaseId);
                    }
                    if (SearchParams.StartDate.HasValue && SearchParams.EndDate.HasValue)
                    {
                        queryBuilder = queryBuilder.Greater(pr => pr.ReturnDate, SearchParams.StartDate.Value);
                        queryBuilder = queryBuilder.LessThanEqual(pr => pr.ReturnDate, SearchParams.EndDate.Value);
                    }
                    if (SearchParams.SupplierCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(pr => pr.Supplier.Id, SearchParams.SupplierCode);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }
    }
}
